<%--
  Created by IntelliJ IDEA.
  User: Rui
  Date: 2018/10/13
  Time: 14:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <%@include file="../../../resources/inc/head.jsp" %>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">设备号</label>
                    <div class="layui-input-block">
                        <input type="text" name="codeDevice" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="thirdSmsCode" lay-filter="thirdSmsCode"></table>
            <script type="text/html" id="optionToolbar">
                <div class="layui-btn-container">
                   <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="dels">批量删除</button>
                </div>
            </script>
            <script type="text/html" id="gmtModifiedTpl">
                {{#
                var date = new Date();
                date.setTime(d.gmtModified);
                return date.Format("yyyy-MM-dd hh:mm:ss");
                }}
            </script>
            <script type="text/html" id="expiryTime">
                {{#
                var date = new Date();
                date.setTime(d.expiryTime);
                return d.codeTimeStamp;
                }}
            </script>
            <script type="text/html" id="isUsed">
                {{#
                var date = new Date();
                date.setTime(d.expiryTime);
                return  d.isUsed==1?'已使用':new Date()-date>0?'已过期':'未使用';
                }}
            </script>
            <script type="text/html" id="isDisabledTpl">
                <input type="checkbox" name="lock" value="{{d.id}}" title="禁用" lay-filter="isDisabledFilter" {{
                       d.isDisabled== 1 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="optionBar">
               <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

<%@include file="../../../resources/inc/footer.jsp" %>
<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;
        table.render({
            elem: '#thirdSmsCode',
            url: '<%=basePath%>manager/third/smsCode/list.action',
            toolbar: '#optionToolbar',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', width: 100, title: 'ID', sort: true}
                , {title: '流水号', sort: true, field: "referenceCode"}
                , {field: 'codeDevice', title: '设备'}
                , {field: 'code', title: '验证码'}
                , {title: '发送时间',templet: "#gmtModifiedTpl"}
                , { title: '状态',templet: "#isUsed"}
                , {title: "操作", width: 180, align: "center", fixed: "right", toolbar: "#optionBar"}
            ]],
            page: !0,
            limit: 10,
            height: "full-130",
            text: {none: "暂无此数据"},
            response: {
                statusCode: 200
            }
        });
        //监听单元格编辑
        table.on('edit(thirdSmsCode)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
        });
        table.on('toolbar(thirdSmsCode)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'dels':
                    var data = checkStatus.data;
                    var List = JSON.stringify(data);
                    var jsList = JSON.parse(List);
                    if (jsList[0] == null) {
                        layer.msg('请选中至少一行');

                    } else {


                        var chars = new Array();
                        for (var li in jsList) {
                            chars[li] = jsList[li].id;
                        }
                        var str = "";
                        for (var i = 0; i < chars.length; i++) {
                            if (i == chars.length - 1) {
                                str += chars[i];
                            }
                            else {
                                str = str + chars[i] + ",";
                            }

                        }
                        layer.load(2);
                        $.ajax({
                            type: "get",
                            url: "<%=basePath%>manager/third/smsCode/delete/" + str + ".action",
                            success: function (data) {
                                layer.closeAll("loading");
                                data = JSON.parse(data);


                                if (data.flag) {
                                    table.reload('thirdSmsCode');
                                    layer.msg("删除成功");
                                } else {
                                    layer.msg(data.msg, {icon: 5});
                                }
                            }
                        });
                        break;
                    }
                    break;
            }
            ;
        });
        table.on("tool(thirdSmsCode)", function (e) {
            console.log(e.data)
            if ("del" === e.event) {
                layer.confirm("真的删除行么", function (t) {
                    layer.close(t);
                    var List = JSON.stringify(e.data);
                    var jsList = JSON.parse(List);
                    var ids = jsList.id;
                    layer.load(2);
                    $.ajax({
                        type: "get",
                        url: "<%=basePath%>manager/third/smsCode/delete/" + ids + ".action",
                        success: function (data) {
                            layer.closeAll("loading");
                            data = JSON.parse(data);


                            if (data.flag) {
                                table.reload('thirdSmsCode');

                            } else {
                                layer.msg(data.msg, {icon: 5});
                            }
                        }
                    });

                })
            }
        });

        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            $(".layui-laypage-skip").find("input").val("1");
            $(".layui-laypage-btn").click();
            table.reload('thirdSmsCode', {
                where: field
            });
        });
        //监听锁定操作

    });


</script>
</body>
</html>

